package com.example.coursemanagement.controller;


import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.MyUtils.ResultCode;
import com.example.coursemanagement.mapper.CatalogueMapper;
import com.example.coursemanagement.pojo.Catalogue;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangmenghen
 * @since 2020-06-01
 */
@RestController
@RequestMapping("/catalogue" )
@RequiredArgsConstructor
public class CatalogueController{

    private final CatalogueMapper catalogueMapper;

    @ApiOperation("根据课程id获取课程章节")
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public CommonResult<Object> queryAllCatalogue(Catalogue catalogue){
        Map<String,Object> map = new HashMap<>();
        map.put("course_id",catalogue.getCourseId());
        Collection<Catalogue> catalogues = catalogueMapper.selectByMap(map);
        if(catalogues==null){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            return CommonResult.success(catalogues);
        }
    }

    @ApiOperation("添加章节")
    @RequestMapping(value = "/addCatalogue",method = RequestMethod.POST)
    public CommonResult<Object> addCatalogue(Catalogue catalogue){
        if(catalogueMapper.insert(catalogue)>0){
            return CommonResult.success(ResultCode.ADD_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("修改章节")
    @RequestMapping(value = "/updateCatalogue",method = RequestMethod.POST)
    public CommonResult<Object> updateCatalogue(Catalogue catalogue){
        if(catalogueMapper.updateById(catalogue)>0){
            return CommonResult.success(ResultCode.UPDATE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("删除章节")
    @RequestMapping(value = "/deleteCatalogue",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteCatalogue(int id){
        if(catalogueMapper.deleteById(id)>0){
            return CommonResult.success(ResultCode.DELETE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

}

